<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Administrator_LeftimgmenuController extends Zend_Controller_Action
{
	

	public function init(){
		
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			$this->_redirect(MY_URL.'/administrator-auth-login');
			
		}
		
	}
	
	public function preDispatch()
	{
		
		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		//$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());
		
	}
	

	public function editAction(){
		
		$imgString=file_get_contents(MY_ROOT.'/html/upload/turn_round/leftimgmenu.txt');
		
		$imgArray=array();
		
		$imgArray=json_decode($imgString,true);

		$this->view->assign('imgArray',$imgArray);
		
	}

	public function logosAction(){

		$imgString=file_get_contents(MY_ROOT.'/html/upload/turn_round/logos.txt');

		$imgArray=array();

		$imgArray=json_decode($imgString,true);

		$this->view->assign('imgArray',$imgArray);

	}
	
	public function doeditAction(){
		
		$this->_helper->viewRenderer->setNoRender();
    	
    	$thisArray=$this->_request->getParams();
		
		$path=new CPath();
		
		$imgArray=array();
			
		for($i=0;$i<count($thisArray['imgUrl']);$i++){
			
			if($thisArray['nochange'][$i]!='1'){
		
				$new_url=$path->toDbPath('item',$thisArray['imgUrl'][$i]);
						
				CFile::copyAndUnlink($path->toAbsoluteRootPath($thisArray['imgUrl'][$i]),$path->toAbsoluteRootPath($new_url));
				
				$thisArray['imgUrl'][$i]=$new_url;
				
			}
			
			$imgArray['imgHref'][]=$thisArray['imgHref'][$i];
			
			$imgArray['imgUrl'][]=$thisArray['imgUrl'][$i];
			
			$imgArray['ordering'][]=$thisArray['ordering'][$i];
			
		}
		
		file_put_contents(MY_ROOT.'/html/upload/turn_round/leftimgmenu.txt',json_encode($imgArray));
		
		$this->view->showMsg("更新成功！",$_SERVER['HTTP_REFERER']);
		
	}

        public function dologosAction(){

		$this->_helper->viewRenderer->setNoRender();

    	$thisArray=$this->_request->getParams();

		$path=new CPath();

		$imgArray=array();

		for($i=0;$i<count($thisArray['imgUrl']);$i++){

			if($thisArray['nochange'][$i]!='1'){

				$new_url=$path->toDbPath('item',$thisArray['imgUrl'][$i]);

				CFile::copyAndUnlink($path->toAbsoluteRootPath($thisArray['imgUrl'][$i]),$path->toAbsoluteRootPath($new_url));

				$thisArray['imgUrl'][$i]=$new_url;

			}

			$imgArray['imgHref'][]=$thisArray['imgHref'][$i];

			$imgArray['imgUrl'][]=$thisArray['imgUrl'][$i];

			$imgArray['ordering'][]=$thisArray['ordering'][$i];

		}

		file_put_contents(MY_ROOT.'/html/upload/turn_round/logos.txt',json_encode($imgArray));

		$this->view->showMsg("更新成功！",$_SERVER['HTTP_REFERER']);

	}
	
    public function uploadimgAction(){
    	
    	header("Content-type: text/html; charset=utf-8");
    	
    	$this->getHelper ('layout')->disableLayout ();
    	  	
    	$upload=new CUpload();
    	
    	$path=new CPath();
    	
    	$thisPath=$path->getPathFromType('temp');
	
    	$imageArray=$upload->uploadFile($thisPath,null,null,'500000');
    	
    	$isSuccess=false;
    	
    	if(is_array($imageArray)){
    		
    		$isSuccess=true;
    		
	    	$imgUrl=$path->toDbPath('temp',$imageArray[0]);
	    	
	    	$this->view->assign('imgUrl',$imgUrl);
	    	
    	}else{
    		
    		$error=$imageArray;
    		
    		$this->view->assign('error',$error);
    		
    	}
    	
    	$this->view->assign('isSuccess',$isSuccess);	
    	
    }
    
	public function delimgAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if(@unlink(MY_ROOT.'/'.$this->_request->getParam('imgurl'))){
    		
    		echo 'success';
    		
    	}else{
    		
    		echo 'success';
    		
    	}
    	
    }

	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
